package cn.net.lwj.singledb.service.impl;

import cn.net.lwj.common.model.BmFy;
import cn.net.lwj.common.model.JsonResult;
import cn.net.lwj.common.model.kingbase.TUser;
import cn.net.lwj.common.model.kingbase.TUserHaskey;
import cn.net.lwj.common.service.Impl.BaseServiceImpl;
import cn.net.lwj.common.util.SpringUtil;
import cn.net.lwj.singledb.mapper.BmFyMapper;
import cn.net.lwj.singledb.mapper.TUserHaskeyMapper;
import cn.net.lwj.singledb.mapper.TUserMapper;
import cn.net.lwj.singledb.mapper.TreeMapper;
import cn.net.lwj.singledb.service.TestService;
import cn.net.lwj.singledb.util.DaoTool;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.generics.tree.Tree;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;


//@log
@Slf4j
@Service
public class TestServiceImpl extends BaseServiceImpl implements TestService {
    @Autowired
    private TreeMapper treeMapper;

    @Resource
    private TUserMapper userMapper;

    @Autowired
    private DaoTool daoTool;

    @Autowired
    private TUserHaskeyMapper userHaskeyMapper;
    @Autowired
    SqlSessionFactory factory;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;


    @Override
    @Transactional
    public JsonResult test() {
        String p1 = "啊啊啊";
        String p2 = "哈哈哈";
        log.info("sss");
        log.info("Processing trade with param1:[{}] and param2: [{}] ", p1, p2);
        log.info("Processing trade with param1:{} and param2: {} ", p1, p2);
        log.debug("啊啊啊嗷嗷嗷嗷param1:{} and param2: {} ", p1, p2);
        log.warn("警告啊啊啊啊啊param1:{} and param2: {} ", p1, p2);
        log.error("错误啊啊啊啊啊param1:{} and param2: {} ", p1, p2);

        List<String> list = new ArrayList();
        list.add("222");
        list.add("6666");
        log.info("{}", list);
        log.info("测试1111[{}]", list);

        log.info("测试啊啊啊{},集合啊啊{}", p2, list, new RuntimeException("测试嘎嘎嘎"));
        testLog();
        try {

            //int a = 5 / 0;
        } catch (Exception e) {
            log.error("错误", e);
        }
        log.info("Processing trade with param1:{} and param2: {} ", p1, p2);

        /*BmFyMapper bmFyMapper = SpringUtil.getBean(BmFyMapper.class);
        List<BmFy> bmFyList = bmFyMapper.selectAll();
        SqlSession session=factory.openSession();
        //List<Map> list=session.selectList(sqlid, i);
        //List<Map> list = session.selectList("", "");
        List<Map<String, Object>> qgfyTree1 = session.getMapper(TreeMapper.class).getQgfyTree("");
        sqlSessionTemplate.getMapper(TreeMapper.class).getQgfyTree("");
        sqlSessionTemplate.getMapper(TreeMapper.class).getQgfyTree("");
        //factory.openSession().getMapper(TreeMapper.class).getQgfyTree("")
        //bmFyMapper.
        BmFy m00 = bmFyMapper.selectByPrimaryKey("M00");
        Example example = new Example(BmFy.class);

        //example.createCriteria().andIn("dmms", "重庆市高级人民法院");
        //example.createCriteria().andEqualTo("dmms", "重庆市高级人民法院");
        //example.createCriteria().andLike("dm", "M1%");
        //example.createCriteria().andCondition("dmms='重庆市高级人民法院'");
        example.createCriteria().andCondition("DM LIKE 'M5_' AND DMMS='綦江县人民法院'");
        //example.createCriteria().andCondition("DM LIKE 'M5_' ");
        List<BmFy> bmFIES = bmFyMapper.selectByExample(example);
        bmFIES.get(0).setDmms("綦江区人民法院");

        bmFyMapper.updateByPrimaryKey(bmFIES.get(0));
        log.info("[{}]", bmFyList);
        log.info("测试[{}]", bmFyList);
        Map<String, Object> map = new HashMap<>();
        map.put("aaa", "aaa");
        map.put("bbb", "bbb");
        log.info("测试[{}]", map);
        log.info("测试{}", map);
        log.info("{}", map);

        BmFy bmFy = bmFyList.get(1);
        log.info("[]{{{}}}", bmFy);
        log.info("{}", bmFy);
        List<Map<String, Object>> qgfyTree = treeMapper.getQgfyTree("114");*/
        //qgfyTree.forEach(System.out::println);
        //动态获取dao
        //BmFyMapper bmFyMapper = (BmFyMapper) SpringUtil.getBean(Class.forName("cn.net.lwj.sjqy.dao.mysql." + daoName));

        return JsonResult.responseSuccess("success", null);

        //userMapper.selectByPrimaryKey()
    }

    @Override
    public JsonResult rdjcTest() {
        List<TUser> tUsers = userMapper.selectAll();
        List<TUserHaskey> tUserHaskeys = userHaskeyMapper.selectAll();
        System.out.println(tUserHaskeys);
        return JsonResult.responseSuccess("获取成功", tUsers);
    }


    /**
     * 根据表名生成实体名
     * @param str
     * @return
     */
    public String createPojoName(String str){
        StringBuilder sb = new StringBuilder();
        String[] ary = str.split("_");
        for (String t : ary) {
            if (t.length() == 1) {
                sb.append(t.toUpperCase());
            }else {
                if ("xf".equals(t)) {
                    t = "wsxf";
                }
                sb.append(t.substring(0, 1).toUpperCase())
                        .append(t.substring(1));
            }
        }
        System.out.println(sb);
        return sb.toString();
    }
}
